package personnel.department.pages.addpage;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;

import com.google.common.collect.Lists;

import personnel.department.dao.entity.Certificate;
import personnel.department.pages.PDTable;
import personnel.department.pages.widgets.AbstractWidget;
import personnel.department.utils.DateUtils;

public class CertificatesEditor extends AbstractTableEditor<Certificate>{

	public CertificatesEditor(CTabFolder tabFolder) {
		super(tabFolder, "Сертификаты");
	}

	public CertificatesEditor(CTabFolder tabFolder, Set<Certificate> certificates) {
		super(tabFolder, "Сертификаты", certificates);
	}

	@Override
	public PDTable<Certificate> createTable(Composite parent) {
		return new PDTable<Certificate>(parent) {

			@Override
			public List<Certificate> setModelProvider() {
				return getModel();
			}

			@Override
			protected void createColumns(Composite parent, TableViewer viewer) {
				String[] titles = { "Серия", "номер сертификата", "Специальность", "Дата присвоения", "Дата подтвержения"};
				int[] bounds = { 100, 130, 100, 120, 120};

				TableViewerColumn col = createTableViewerColumn(getViewer(), titles[0], bounds[0], 0);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Certificate c = (Certificate) element;
						return String.valueOf(c.getSeries());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[1], bounds[1], 1);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Certificate c = (Certificate) element;
						return String.valueOf(c.getNumberCertificate());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[2], bounds[2], 2);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Certificate c = (Certificate) element;
						return c.getSpeciality();
					}
				});

				col = createTableViewerColumn(getViewer(), titles[3], bounds[3], 3);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Certificate c = (Certificate) element;
						return c.getAssignmentCertificate() != null ? 
								format.format(c.getAssignmentCertificate()) : null;
					}
				});

				col = createTableViewerColumn(getViewer(), titles[4], bounds[4], 4);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Certificate c = (Certificate) element;
						return c.getConfirmation() != null ? 
								format.format(c.getConfirmation()) : null;
					}
				});

			}
		};
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void validate() {
		AbstractWidget<?> firstCertificate = getWidgetByFieldName("firstCertificate");
		boolean present = getModel().stream().
				filter(cert -> cert.getFirstCertificate())
				.findAny()
				.isPresent();
		if (present) {
			firstCertificate.setEnabled(false);
		}

		AbstractWidget<Date> confirmation = (AbstractWidget<Date>) getWidgetByFieldName("confirmation");
		AbstractWidget<Date> assignmentCertificate = (AbstractWidget<Date>) getWidgetByFieldName("assignmentCertificate");

		if (!getModel().isEmpty()) {
			Date maxConfirmation = getModel().stream()
					.max((arg0, arg1) -> arg0.getConfirmation().compareTo(arg1.getConfirmation()))
					.map(cert -> cert.getConfirmation())
					.get();

			assignmentCertificate.setValue(maxConfirmation);
			confirmation.setValue(DateUtils.add(maxConfirmation, Calendar.YEAR, 5));
		}
	}


}
